﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SWBetongXaydung.GUI.Common;
using SWBetongXaydung.BLL;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;
using SWBetongXaydung.Utils;
namespace SWBetongXaydung.GUI.ThongKe
{
    public partial class UCThongKe_TongHopThanhToanNhienLieu : BaseUserControl
    {
        public UCThongKe_TongHopThanhToanNhienLieu()
        {
            InitializeComponent();
            var thang = DateTime.Now.Month;
            cmbThang.Text = thang.ToString();
            cmbNam.Text = DateTime.Now.Year.ToString();
            if (thang < 4)
                cmbQuy.Text = "1";
            else if (thang < 7)
                cmbQuy.Text = "2";
            else if (thang < 10)
                cmbQuy.Text = "3";
            else
                cmbQuy.Text = "4";
            rdThang_CheckedChanged(this, null);
        }

        bool Validate_Data()
        {
            errorNotify.Clear();
            int i = 0;
            if (rdThang.Checked == true && !int.TryParse(cmbThang.Text, out i))
            {
                errorNotify.SetError(cmbThang, "Tháng không đúng");
                return false;
            }
            if (rdQuy.Checked == true && !int.TryParse(cmbQuy.Text, out i))
            {
                errorNotify.SetError(cmbQuy, "Quý không đúng");
                return false;
            }
            if (!int.TryParse(cmbNam.Text, out i))
            {
                errorNotify.SetError(cmbNam, "Năm không đúng");
                return false;
            }

            return true;
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            if (Validate_Data())
            {
                int thang = 0, quy = 0;
                if (rdThang.Checked == true)
                    thang = int.Parse(cmbThang.Text);
                else
                    quy = int.Parse(cmbQuy.Text);
                int nam = int.Parse(cmbNam.Text);
                List<ThongKeTongHopThanhToan> list;
                if (rdThang.Checked == true)
                    list = ThongKeTongHopThanhToan.GetListByThang(thang, nam);
                else
                    list = ThongKeTongHopThanhToan.GetListByQuy(quy, nam);
                ThongKeTongHopThanhToanBindingSource.DataSource = list;
                ReportParameter[] parameters = new ReportParameter[2];
                if (rdThang.Checked)
                    parameters[0] = new ReportParameter("rpTieuDe", "THÁNG " + thang + " NĂM " + nam);
                else
                    parameters[0] = new ReportParameter("rpTieuDe", "QUÝ " + quy + " NĂM " + nam);
                parameters[1] = new ReportParameter("rpNgayLapBieu", string.Format("{0:dd/MM/yyyy}", DateTime.Now).DateToText());
                System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
                pg.Margins.Top = 20;
                pg.Margins.Bottom = 20;
                pg.Margins.Left = 20;
                pg.Margins.Right = 20;
                pg.Landscape = true;
                PaperSize size = new PaperSize();
                size.Width = 827;
                size.Height = 1169;
                pg.PaperSize = size;
                rpThongKeTongHop.SetPageSettings(pg);
                rpThongKeTongHop.LocalReport.SetParameters(parameters);
                rpThongKeTongHop.RefreshReport();
            }
        }

        private void rdThang_CheckedChanged(object sender, EventArgs e)
        {
            cmbThang.Visible = cmbThang.Enabled = true;
            cmbQuy.Visible = cmbQuy.Enabled = false;
            lbThangQuy.Text = "Tháng:";

        }

        private void rdQuy_CheckedChanged(object sender, EventArgs e)
        {
            cmbThang.Visible = cmbThang.Enabled = false;
            cmbQuy.Visible = cmbQuy.Enabled = true;
            lbThangQuy.Text = "Quý:";
        }
    }
}
